Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEEL variable scope resolution #43

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

FEEL variable scope resolution #43

wants to merge 5 commits into from

Conversation

abdul99ahad
Copy link
Contributor

@abdul99ahad abdul99ahad commented Dec 16, 2024

Related to: camunda-modeler #4737

Proposed Changes

List of changes related to scope:

  • Only resolve result variable globally if no output mapping exists.
  • Resolve result variable locally if output mapping exists.
  • Result variable and input have same names, result variable will be available throughout overwriting input variable.
  • Result variable and output have same names, output variable will be available overwriting result variable.
  • Input and output have same names, output variable name will overwrite the input variable.
  • When multiple input or output variables with same name exists within the same process/activity, the later one overwrites all the previous ones.

Visual demo

When script result variable and input with same names coexist, result variable overwrites the input variable.
Dec-16-2024 23-19-02

When same name input/output mapping exists, the later one overwrites the previous one.
Dec-16-2024 23-19-50

You can test the implementation by running this command: npx @bpmn-io/sr bpmn-io/variable-resolver#variable-scoping -l bpmn-io/extract-process-variables#main

Checklist

To ensure you provided everything we need to look at your PR:

  • Brief textual description of the changes present
  • Visual demo attached
  • Steps to try out present, i.e. using the @bpmn-io/sr tool
  • Related issue linked via Closes {LINK_TO_ISSUE} or Related to {LINK_TO_ISSUE}

@bpmn-io-tasks bpmn-io-tasks bot added the needs review Review pending label Dec 16, 2024
@abdul99ahad

This comment was marked as outdated.

@abdul99ahad abdul99ahad requested review from nikku and barmac December 16, 2024 18:24
@abdul99ahad abdul99ahad marked this pull request as draft December 16, 2024 18:28
@bpmn-io-tasks bpmn-io-tasks bot added in progress Currently worked on and removed needs review Review pending labels Dec 16, 2024
@nikku
Copy link
Member

nikku commented Jan 2, 2025

Upstream fix released, this can be moved further.

@abdul99ahad abdul99ahad force-pushed the variable-scoping branch 4 times, most recently from 7d79e35 to b55efe3 Compare January 7, 2025 11:05
@abdul99ahad abdul99ahad marked this pull request as ready for review January 7, 2025 11:18
@bpmn-io-tasks bpmn-io-tasks bot added needs review Review pending and removed in progress Currently worked on labels Jan 7, 2025
@@ -198,7 +198,7 @@ function getIoExpression(variable, origin) {
return;
}

const mapping = mappings.find(mapping => mapping.target === variable.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd apprechiate if we'd group the fix with the actual test that verifies the fix. This way it is easier to understand what we fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix corresponds to

When multiple input or output variables with same name exists within the same process/activity, the later one overwrites all the previous ones.

I'll see if the test case doesn't exist for this, I'll add one. Thanks for pointing this out.

@@ -59,6 +58,12 @@ export default class ZeebeVariableResolver extends BaseVariableResolver {
// Filter all pre-defined variables
return !namesToFilter.includes(v.name);
});

// keep only unique names
const uniqueVariables = new Map();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified in some ways (I think):

// use min-dash uniqueBy utility
const uniqueVariables = uniqueBy('name', filteredVariables.reverse());
const uniqueVariables = Object.values(
  filteredVariables.reduce((uniqueVariables, v) =>  {
    uniqueVariables[v.name] = v;
    return uniqueVariables;
  })
);

Maybe it even makes sense to do the filtering in the previous block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented. The variables are now filtered distinctly using uniqueBy from min-dash in the base VariableResolver.

@@ -198,7 +198,7 @@ function getIoExpression(variable, origin) {
return;
}

const mapping = mappings.find(mapping => mapping.target === variable.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also search mappings from last to first:

mappings.reverse().find(...);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any particular reason we're sticking to find(...) and avoid filter(...).pop() ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Find aborts early, while filter goes through the whole list. But in the end it is a matter of taste.

pop() is also not side-effect free, so it is not fully functional, and may cause (in some situations) issues.

@abdul99ahad abdul99ahad force-pushed the variable-scoping branch 5 times, most recently from e87fb60 to 6e3dca3 Compare January 8, 2025 10:33
@abdul99ahad abdul99ahad requested a review from nikku January 8, 2025 10:35
@@ -198,7 +198,7 @@ function getIoExpression(variable, origin) {
return;
}

const mapping = mappings.find(mapping => mapping.target === variable.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Find aborts early, while filter goes through the whole list. But in the end it is a matter of taste.

pop() is also not side-effect free, so it is not fully functional, and may cause (in some situations) issues.

@bpmn-io-tasks bpmn-io-tasks bot added in progress Currently worked on and removed needs review Review pending labels Jan 8, 2025

namesToFilter.push(...outputsToFilter);
}
const namesToFilter = getElementNamesToRemove(moddleElement, inputOutput);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with other commit in this PR, what problem does this fix solve? I'd apprechiate if we'd pack implementation + test case together.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a fix, just a refactor to clean up the code into a separate function. I'll make sure the commits are clear for reviewers.

Copy link
Member

@nikku nikku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abdul99ahad Thanks for doing this additional round. I still find it a little bit hard to review your PR, because some "FIX" commits still don't contain tests for the actual bugs fixed.

For any reasonably complex topic (such as this one) we need rigid test coverage. I advice you to adopt the practice to pack fix + test into one commit. This helps reviewers to trace what you were doing, and what exactly the expected effect is.

@abdul99ahad
Copy link
Contributor Author

@abdul99ahad Thanks for doing this additional round. I still find it a little bit hard to review your PR, because some "FIX" commits still don't contain tests for the actual bugs fixed.

I advice you to adopt the practice to pack fix + test into one commit. This helps reviewers to trace what you were doing, and what exactly the expected effect is.

Thanks for pointing this out, Nico. I’ll make sure to include tests alongside fixes in a single commit for better traceability in the future. For now, I’ve separated the refactor into its own commit to keep things clearer.

@nikku
Copy link
Member

nikku commented Jan 9, 2025

Ok, I'll see what I can do, and update this PR with co-location. Will ping you to check back with you, if I grouped things appropriately.

@abdul99ahad
Copy link
Contributor Author

Ok, I'll see what I can do, and update this PR with co-location. Will ping you to check back with you, if I grouped things appropriately.

I have grouped the fixes and tests. 🏅

@abdul99ahad abdul99ahad requested a review from nikku January 11, 2025 14:36
@bpmn-io-tasks bpmn-io-tasks bot added needs review Review pending and removed in progress Currently worked on labels Jan 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs review Review pending
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants